package com.example.everying.utils;/*
 * 项目名称: everything
 * 文件名: IpUtils
 * 作者: 张泽阳
 * 日期: 2025/6/3
 * 描述:
 */

import jakarta.servlet.http.HttpServletRequest;
public class IpUtils {
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty()  || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.isEmpty()  || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 对于多级代理的情况，取第一个IP
        if (ip != null && ip.contains(","))  {
            ip = ip.substring(0,  ip.indexOf(",")).trim();
        }
        return ip;
    }
}
